package com.adolph.netty.nettydemo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * 〈功能概述〉<br>
 *
 * @author: wangxiangjun
 * @date: 2020-10-09 9:12
 */

public class NettyServer {

    public static void main(String[] args) {
        //
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            //启动器
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128)//设置线程队列得到连接个数
                    .childOption(ChannelOption.SO_KEEPALIVE,Boolean.TRUE)//设置保持活动连接状态
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        //给pipeline设置处理器
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //可以获取每个客户端对应的Channel
//                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
                            //基于分隔符的协议，将会更具协议提取帧发给下一个handler
                            ByteBuf delimiter = Unpooled.copiedBuffer("$".getBytes("UTF-8"));
                            ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024000, delimiter));
                            //连接超过60秒没有接收和发送任何消息将会触发fireUserEventTriggered()方法，即下一个handler的userEventTriggered()方法将会被调用
                            ch.pipeline().addLast(new IdleStateHandler(0,0,60, TimeUnit.SECONDS));
                            ch.pipeline().addLast(new HeartbeatHandler());
                            ch.pipeline().addLast(new StringToObjDecoder());
                            ch.pipeline().addLast(new NettyServerHandler());
                            ch.pipeline().addLast(new NettyServerHandlerNext());
                            ch.pipeline().addLast(new NettyServerHandlerLast());
                        }
                    });
            //绑定端口并且同步
            ChannelFuture sync = bootstrap.bind(8888).sync();
            //Feature-listener机制：注册监听器，处理关心的事情
            sync.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {

                }
            });


            //对关闭通道进行监听
            sync.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            workGroup.shutdownGracefully();
        }



    }
}
